package com.itfreer.bpm.workflow.service.back;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.bpm.config.service.IBpmConfigService;
import com.itfreer.bpm.config.service.IBpmTaskConfigService;
import com.itfreer.bpm.flow.api.IBpmProjectEntity;
import com.itfreer.bpm.flow.api.IUserNextTaskUser;
import com.itfreer.bpm.history.entity.BpmHistoryEntity;
import com.itfreer.bpm.messager.BpmMessage;
import com.itfreer.bpm.option.para.NextTaskInfo;
import com.itfreer.bpm.option.para.OptionInfo;
import com.itfreer.bpm.option.para.OptionType;
import com.itfreer.bpm.option.para.TaskInfo;
import com.itfreer.bpm.option.para.TaskType;
import com.itfreer.bpm.power.para.UserInfo;
import com.itfreer.bpm.workflow.drive.IWorkFlowDrive;
import com.itfreer.bpm.workflow.option.SignCommonTask;
import com.itfreer.bpm.workflow.option.SignMultiTask;
import com.itfreer.bpm.workflow.service.BpmWorkFlowCommonSignImp;
import com.itfreer.bpm.workflow.userrole.IUserRole;
import com.itfreer.bpm.workflow.utils.BpmApprovalHistoryUtil;
import com.itfreer.bpm.workflow.utils.BpmHistoryEditUtil;
import com.itfreer.bpm.workflow.utils.BpmHistoryQueryUtil;
import com.itfreer.bpm.workflow.utils.BpmNextTaskUtils;
import com.itfreer.bpm.workflow.utils.BpmUserAuthUtils;
/**
 * 流程退回实现类
 * @author gj
 *
 */
@Component
public class BpmWorkFlowBackImp {
	/**
	 * 流程驱动服务
	 */
	@Autowired
	protected IWorkFlowDrive workFlowDrive;
	
	/**
	 * 构建历史帮转工具
	 */
	@Autowired
	BpmHistoryEditUtil historyEditUtl;
	
	/**
	 * 构建用户审批历史
	 */
	@Autowired
	BpmApprovalHistoryUtil approvalUtil;
	
	/**
	 * 用户授权工具
	 */
	@Autowired
	BpmUserAuthUtils userAuthUtils;
	
	/**
	 * 审批过程查询接口
	 */
	@Autowired
	BpmHistoryQueryUtil historyQueryUtil;
	
	/**
	 * 用户角色接口
	 */
	@Autowired
	IUserRole userRole;
	/**
	 * 用户自定义操作下一步审批人接口
	 */
	@Autowired(required=false)
	IUserNextTaskUser userNextTaskUser;
	
	/**
	 * 下一个环节工具
	 */
	@Autowired
	BpmNextTaskUtils nextTaskUtils;
	/**
	 * 单节点会签提交
	 */
	@Autowired
	SignMultiTask signmultiTask;
	
	/**
	 * 通用流程驱动操作
	 */
	@Autowired
	BpmWorkFlowCommonSignImp commonSign;
	
	/**
	 * 多节点选择节点
	 */
	@Autowired
	SignCommonTask signCommonTask;
	
	public OptionInfo getBackTask(String bpmkey, IBpmProjectEntity entity, List editHistory, UserInfo user) {
		OptionInfo info=new OptionInfo();
		info.setbpmkey(bpmkey);
		String sexeid=entity.getSexeid();
		info.setSexeid(sexeid);
		
		HashMap<String,Object> work_where=new HashMap<String,Object>();
		work_where.put("sexeid:=", sexeid);
		//检查校验是否是超级管理员
		if(!user.isSuperAdmin()) {
			work_where.put("toptassigneese:like",user.getUserId());
		}
		List<BpmHistoryEntity>toWorkList= historyQueryUtil.getWorkEntitys(null, work_where, null, 1000, 1);
		
		//处理当前环节
		List<TaskInfo> ctasks=new ArrayList<TaskInfo>();
		for(BpmHistoryEntity work:toWorkList) {
			TaskInfo task=new TaskInfo();
			task.setTaskdefinedid(work.gettaskdefid());
			task.setTaskName(work.gettaskdefname());
			ctasks.add(task);
		}
		info.setcTask(ctasks);
		
		//处理掉下一个环节信息
		String ctaskid=null;
		if(toWorkList!=null && toWorkList.size()>0) {
			ctaskid=toWorkList.get(0).gettaskdefid();
		}
		NextTaskInfo nextTaskInfo= this.nextTaskUtils.getRollBackTaskInfo(sexeid,bpmkey, ctaskid,entity);

		if(nextTaskInfo.getNextTask()!=null && nextTaskInfo.getNextTask().size()>0 && nextTaskInfo.getNextTask().size()<2) {
			info.setOptiontype(OptionType.CommonBack);
			nextTaskInfo.getNextTask().get(0).setTaskSelect(true);
		}else if(nextTaskInfo.getNextTask()!=null && nextTaskInfo.getNextTask().size()>1){
			info.setOptiontype(OptionType.ExclusiveBack);
			nextTaskInfo.getNextTask().get(0).setTaskSelect(true);
		}
		//设计节点退回方式
		info.setNextTask(nextTaskInfo.getNextTask());
		return info;
	}
	
	/**
	 * 项目退回功能的节点开发配置
	 */
	public BpmMessage sendBackTask(OptionInfo oInfo, IBpmProjectEntity entity, UserInfo user) {
		if(oInfo.getNextTask()!=null && oInfo.getNextTask().size()>0) {
			for(TaskInfo itask: oInfo.getNextTask()) {
				if(itask.isTaskSelect()) {
					HashMap<String,Object> history_where=new HashMap<String,Object>();
					history_where.put("sexeid:=", oInfo.getSexeid());
					history_where.put("taskdefid:=", itask.getTaskdefinedid());
					
					HashMap<String,Integer> history_order=new HashMap<String,Integer>();
					history_order.put("opttime", 0);
					List<BpmHistoryEntity> hlst=this.historyQueryUtil.getEntitys(null, history_where, history_order, 1, 1);
					
					if(hlst!=null && hlst.size()>0) {
						UserInfo nUser=new UserInfo();
						BpmHistoryEntity iHistory=hlst.get(0);
						nUser.setUserId(iHistory.getoptassigneesid());
						nUser.setUserName(iHistory.getoptassigneesname());
						nUser.setUserOrgCode(iHistory.getoptorgid());
						nUser.setUserOrgCode(iHistory.getoptorgname());
						nUser.setUserSelected(true);
						itask.getUser().add(nUser);
					}else {
						user.setUserSelected(true);
						itask.getUser().add(user);
					}
				}
			}
		}
		
		return this.commonSign.signTask(oInfo, entity, user, "退回");
	}
}
